package com.lee.senior;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class MyMapper {


    /**
     * 根据单个商品计算中销售量
     */
    public static class TotalMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

        @Override
        protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {

            String line = value.toString();
            if(line == null || line.startsWith("#") || line.isEmpty()) {
                return;
            }
            String[] words = line.split("\\s+");
            System.out.println("mapper------------" + words[0] + ":" + words[2]);
            context.write(new Text(words[0]), new IntWritable(Integer.parseInt(words[2])));
        }
    }

    /**
     * 根据销量排序
     */
    public static class SaleSortMapper extends Mapper<LongWritable, Text, WcDo, IntWritable> {

        @Override
        protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {

            String line = value.toString();
            if(line == null || line.startsWith("#") || line.isEmpty()) {
                return;
            }
            String[] words = line.split("\\s+");

            context.write(new WcDo(words[0], Integer.parseInt(words[1]), Integer.parseInt(words[2])), new IntWritable(Integer.parseInt(words[2])));
        }
    }
}


